package com.examinantsdk.base;

import android.annotation.SuppressLint;
import android.util.Log;

import androidx.annotation.NonNull;

import org.jetbrains.annotations.NotNull;

/**
 * Create by liuwei on 2021/6/29.
 */
public class CrashHandler implements Thread.UncaughtExceptionHandler {
    private static volatile CrashHandler crashHandler;
    private CrashHandler(){
    }

    public void init(){
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    public static CrashHandler getCrashHandler(){
        if (crashHandler == null){
            synchronized (CrashHandler.class){
                if (crashHandler == null){
                    crashHandler = new CrashHandler();
                }
            }
        }
        return crashHandler;
    }

    @SuppressLint("LogNotTimber")
    @Override
    public void uncaughtException(@NonNull @NotNull Thread t, @NonNull @NotNull Throwable e) {
        Log.e("CrashHandler", "UncaughtException：" + e.getMessage());
        e.printStackTrace();
        BaseAppManager.getInstance().clear();
        //退出程序
        android.os.Process. killProcess(android.os.Process.myPid());
        System.exit(0);
    }
}
